#include "GoalData.h"
#include "CG6-HeartRate.h"
#include "CG6-Generator.h"
#include  "Parameter.h"
#include "CG6-Calc.h"


CGoalData::CGoalData()
{
	m_dblWatts    = 0;
	m_dwPWM		  = 0;
	m_dwRPM		  = 0;
	m_dblCalories = 0;
	m_dblSpeed	  = 0;

	m_dblMETs	  = 0;
	m_dblWatts	  = 0 ;
	m_dblCalories	  = 0;
	m_dblDistance	  = 0;
	m_nHeartRate	  = 0;

	m_nThisCount	  = 0;
	m_nLastCount	  = 0;
	m_nTotalCount	  = 0;

	m_dwPWM  = 0;
	m_dwRPM  = 0;

}

CGoalData::~CGoalData()
{

}

BOOL	CGoalData::LoadSetting()
{
	//CConfig* pCFG = CConfig::GetInstance();
	//m_UserInfo = pCFG->Get_UserInfo();
	//m_dwDiffLev = pCFG->GetDifficulty_level();
	//SetUserInfo( m_UserInfo.m_nMale,m_UserInfo.m_nEnter_Age,m_UserInfo.m_nEnter_Weight,m_UserInfo.m_bKG );
	return TRUE;
}

void	CGoalData::SetUserInfo( int nMale,int nAge,int nWeight,BOOL bKG )
{
	m_nMale = nMale;				
	m_nEnter_Age = nAge;
	m_nEnter_Weight = nWeight;
	m_bKG = bKG;
}

void	CGoalData::GenerateHeatrate()
{
	static int count=0;
	int nHeartRate = GetHeartRate();
	if (nHeartRate!=0){
		m_nHeartRate = nHeartRate ;
		count=0;
	}
	else
	{
		count++;
		if (count>4)
		{
			m_nHeartRate = nHeartRate ;
		}
	}
}

void	CGoalData::GenerateCount( BOOL bChangeNext )
{
	BYTE btCount =  GenReadCount();
	btCount = min( btCount&0xF,(btCount&0xF0)>>4);
	m_nThisCount += btCount;
	m_nTotalCount += btCount;
	
	if ( bChangeNext )
	{
		m_nLastCount = m_nThisCount;
		m_nThisCount = 0;
	}
}

BOOL	CGoalData::GenerateCurData()
{
	m_dwRPM = GenReadRPM ( ); 
	m_dblWatts = WATTcalc( m_dwRPM, m_dwPWM );
	m_dblMETs = m_dblWatts * 0.0143 / 5.05 * m_nEnter_Weight / 3.5;
	double dwCaloriesPerSec = m_dblWatts * 0.0143 / 60;
	m_dblCalories += dwCaloriesPerSec;

	m_dblSpeed = m_dwRPM * 0.242 * 8.73 * 3.1416 * 60 / 1000;
	m_dblDistance += m_dblSpeed / 3600;

	return TRUE;
	//RETAILMSG(1,(L"####### m_dwCalories=%d,dwWatts=%d,dMETs=%d,dwRPM=%d,m_dDistance=%d,dSpeed=%d r\n",m_dwCalories,dwWatts,dMETs,dwRPM,m_dDistance,dSpeed ));
}